using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TN5Y.Server;
//using TN5Y.Server;

namespace TeamTime
{
    public partial class ProcessStatusControl : System.Web.UI.UserControl
    {

        public event System.EventHandler ProcessFinished;
        private void ProcessStatusControl_ProcessFinished(object sender, EventArgs e) { }
        public delegate void ProcessFinishedEventHandler(object sender, EventArgs e);

        private string processMethodName;
        private string startTaskScript;

        public string ProcessTitle
        {
            get { return this.lblProcessTitle.Text; }
            set { lblProcessTitle.Text = value; }
        }

        //Can be left blank.  If set, will navigate to the specified URL on successful completion of the process.
        public string NavigateToURLOnCompletion
        {
            get { return txtNavigateToURLOnCompletion.Text; }
            set { txtNavigateToURLOnCompletion.Text = value; }
        }

        public string ProcessGUID 
        { 
            get { return txtGUID.Text; } set { txtGUID.Text=value;} 
        }

        ProgressMonitor ProgressMonitor = new ProgressMonitor();
        public string ProcessMethodName
        {
            get { return processMethodName; }
            set
            {
                processMethodName = value;
                if (txtGUID.Text == "") //process not yet started
                {
                    this.txtGUID.Text = ProgressMonitor.StartProcess(processMethodName, "Starting...", 0);
                }
                startTaskScript = " " +
                "function startTask() " +
                "{  " + "\r\n" +
                "   //alert('Starting task...'); " + "\r\n" +
                "   // Start the remote task using a Web service call" + "\r\n" +
                "   var GUID = '" + this.txtGUID.Text + "';" + "\r\n" +
                "   TeamTime.LongRunningProcesses." + processMethodName + "(GUID, taskCompleted, taskFailed);" + "\r\n" +
                "   // Turn on the progress monitor" + "\r\n" +
                "   updateUI(true); " + "\r\n" +
                "   progressManager.startMonitor(GUID, 1500, updateProgress, updateProgressCompleted); " + "\r\n" +
                "} " + "\r\n" +
                " pageLoad();" + "\r\n" +
                " startTask(); " + "\r\n";
                Type cstype = this.GetType();
                String csname1 = "Script1";
                // Get a ClientScriptManager reference from the Page class.
                ClientScriptManager cs = Page.ClientScript;

                // Check to see if the startup script is already registered.
                if (!cs.IsStartupScriptRegistered(cstype, csname1))
                {
                    cs.RegisterStartupScript(cstype, csname1, startTaskScript, true);
                }


            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnCompleted_Click(object sender, EventArgs e)
        {
            this.ProcessFinished(this, new EventArgs());
        }
    }
}